package com.lanou3g.demo.base.controller;

import com.lanou3g.demo.base.service.AdminInfoService;
import com.lanou3g.demo.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin.do")
public class AdminInfoController {

    @Resource
    private AdminInfoService adminInfoService;

    @RequestMapping("findAll")
    public String findAll(Model model) {
        System.out.println("AdminInfoController.findAll");
        List<AdminInfoQx> adminInfoList = adminInfoService.queryAdminAndRole();
        System.out.println(adminInfoList);
        model.addAttribute("adminInfoList", adminInfoList);

        return "/admin/admin_list";
    }

    @RequestMapping("add")
    public String add(AdminInfo adminInfo, HttpServletRequest request) {

        System.out.println("AdminInfoController.add");
        adminInfoService.insertAdminUserMessages(adminInfo);

        List<AdminRole> adminRoles = new ArrayList<>();

        String[] roleIds = request.getParameterValues("roleId");
        for (String roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setRoleId(Long.parseLong(roleId));
            adminRole.setAdminId(adminInfo.getAdminId());
            adminRoles.add(adminRole);
        }

        adminInfoService.insertAdminRoles(adminRoles);
        return "redirect:/admin.do/findAll";
    }

    @RequestMapping("queryRoleInfo")
    @ResponseBody
    public List<RoleInfo> queryRoleInfo() {
        List<RoleInfo> roleInfos = adminInfoService.queryRoleInfo();

        return roleInfos;
    }

    @RequestMapping("queryAdminInfo")
    public String queryAdminInfo(AdminInfo adminInfo, Model model) {
        AdminInfo adminInfo1 = adminInfoService.queryAdminInfo(adminInfo);
        model.addAttribute("adminInfo", adminInfo1);
        return "/admin/admin_modi";
    }

    @RequestMapping("updateAdminData")
    public String updateAdminData(AdminInfo adminInfo,HttpServletRequest req) {
        System.out.println("AdminInfoController.updateAdminData");
//        String adminId = req.getParameter("adminId");
//        adminInfo.setAdminId(Long.parseLong(adminId));
        System.out.println(adminInfo.getAdminId());
        List<AdminRole> adminRoles = new ArrayList<>();

        String[] roleIds = req.getParameterValues("roleId");
        for (String roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setRoleId(Long.parseLong(roleId));
            adminRole.setAdminId(adminInfo.getAdminId());
            adminRoles.add(adminRole);
        }

        adminInfoService.updateAdminData(adminInfo,adminRoles);
        System.out.println(adminInfo);
        return "redirect:/admin.do/findAll";
    }

    @RequestMapping("deleteAdminRole")
    public String deleteAdminRole(AdminInfo adminInfo) {
        adminInfoService.deleteAdmin(adminInfo);
        return "redirect:/admin.do/findAll";
    }

    @RequestMapping("/queryRoleName")
//    地址加/
    public String queryRoleName(RoleInfo roleInfo, Model model) {
        System.out.println("AdminInfoController.queryRoleName");
        System.out.println(roleInfo.getName());
        List<AdminInfoQx> adminInfoList = adminInfoService.queryRoleName(roleInfo);

        model.addAttribute("adminInfoList", adminInfoList);
        return "/admin/admin_list";
    }

    @RequestMapping(value = "queryRoleId", method = RequestMethod.POST)
    @ResponseBody
    public List<AdminInfoQx> queryRoleId(@RequestBody RoleModule roleModule, Model model) {
//        可以这样传参
        System.out.println("AdminInfoController.queryRoleId");
//        System.out.println(roleModule.getRoleId());
//        System.out.println(moduleId);
//        RoleModule roleModule = new RoleModule();
//        roleModule.setModuleId(moduleId);
//        long moduleId = roleModule.getModuleId();
//        System.out.println(moduleId);
        List<RoleModule> roleModuleList = adminInfoService.queryRoleId(roleModule);
        System.out.println(roleModuleList);

        List<RoleInfo> roleInfoList = adminInfoService.queryRoleNameById(roleModuleList);
        System.out.println(roleInfoList);

        List<AdminInfoQx> adminInfoList = adminInfoService.queryRoleNames(roleInfoList);
        model.addAttribute("adminInfoList", adminInfoList);
//   ajax需要一个请求完成返回的数据
//        return "success";
        return adminInfoList;
//        return "/admin/admin_list"
    }

    @RequestMapping("/updatePassword")
    @ResponseBody
//    请求头
    public String updatePassword(@RequestBody List<Integer> adminIds) {
//        请求体
        adminInfoService.updatePassword(adminIds);

        return "success";

    }

    @RequestMapping("/queryModuleName")
    @ResponseBody
    public List<ModuleInfo> queryModuleName() {
        return adminInfoService.queryModuleName();
    }


    @RequestMapping("findByPage")
    public String findByPage(Model model, HttpServletRequest request) {
        // 查找所有数据 走Mapper
        List<AdminInfoQx> list = adminInfoService.queryAdminAndRole();
        // 所有数据集合长度获取 做为数据总数
        int total = list.size();
        // 用数据总数除每个页面显示的条数 即共有多少页
        int totalPage = total / 5;
        if ((total %= 5) != 0) {
            totalPage += 1;
        }
        // 把总页数穿到前端作为循环长度
        model.addAttribute("totalPage", totalPage);
        // 首先判断是否有page值 如果没有默认加载第一页 如果有就拿到page的值
        if (request.getParameter("page") != null) {
            // 把page转换成数字 即当前页的数字
            Integer currentPage = Integer.valueOf(request.getParameter("page"));
            if (currentPage > 0 && currentPage <= totalPage) {
                // 根据当前页查询数据 公式就是 当前页-1 * 每页条数
                List<Service> accountServiceByPage = adminInfoService.findByPage((currentPage - 1) * 5);
                // 把数据集合传回前端遍历输出
                // 因为不同情况需要不同处理 所以写了很多分支 暂时想不到办法解决
                // 但总的来说应该就这几种情况了
                model.addAttribute("adminInfoList", accountServiceByPage);
                model.addAttribute("currentPage", currentPage);
            }
            // 当前页大于总页数 也就是在最后一页点下一页的傻子
            else if (currentPage >= totalPage) {
                List<Service> firstList = adminInfoService.findByPage((totalPage - 1) * 5);
                model.addAttribute("adminInfoList", firstList);
                model.addAttribute("currentPage", totalPage);
                return "/admin/admin_list";
            }
            // 同上 在第一页点上一页的傻子
            else if (currentPage <= 0) {
                List<Service> firstList = adminInfoService.findByPage(0);
                model.addAttribute("adminInfoList", firstList);
                model.addAttribute("currentPage", 1);
                return "/admin/admin_list";
            } else {
                List<Service> firstList = adminInfoService.findByPage(0);
                model.addAttribute("adminInfoList", firstList);
                model.addAttribute("currentPage", 0);
                return "/admin/admin_list";
            }
            return "/admin/admin_list";
        }
        // 没有page值 所以默认显示第一页 同上一个
        else {
            List<Service> firstList = adminInfoService.findByPage(0);
            model.addAttribute("serviceList", firstList);
            model.addAttribute("currentPage", 0);
            return "/admin/admin_list";
        }

    }
}
